package com.wenwu.controller.movable;

import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsMCulture;
import com.wenwu.common.page.PageUtils;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.common.utils.JsonUtils;
import com.wenwu.common.utils.Status.AttachmentFrom;
import com.wenwu.service.CommonService;
import com.wenwu.service.MoveableCultureService;

/**
 * 文化面貌表
 * 
 * @author Yaphet
 *
 */
@Controller(value = "MoveableCultureController")
@RequestMapping(value = "/mgr/mculture")
public class MoveableCultureController {
	
	Logger log = Logger.getLogger(this.getClass());
	
	private JsonGenerator jsonGenerator;
	
	@Autowired
	private MoveableCultureService moveableCultureService;
	
	@Autowired
	private CommonService commonService;
	
	/**
	 * 初始化界面
	 * @return
	 */
	@RequestMapping(value = "/init")
	public String initUserPage() {
		log.info("进入基本信息管理页面...");
		return "mculture/init";
	}
	
	/**
	 * 可移动文物登记表表单查询
	 * @return
	 */
	@RequestMapping(value = "/showList")
	public void showList(RelicsMCulture relicsMCulture,Model model, HttpServletRequest request,
			HttpServletResponse response) {
		log.info("查询可移动文物登记表表单..." );
		PaginationVO pageVo = PageUtils.getPaginationInfo(request);
		try {
			if(relicsMCulture!=null && relicsMCulture.getmRelicsNo() !=null){
				relicsMCulture.setmRelicsNo(relicsMCulture.getmRelicsNo().trim());
			}
			if(relicsMCulture!=null && relicsMCulture.getRelicsName() !=null){
				relicsMCulture.setRelicsName(relicsMCulture.getRelicsName().trim());
			}
			
			pageVo = moveableCultureService.selectByCondition(relicsMCulture, pageVo);
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			jsonGenerator.writeObject(pageVo);// 将PaginationVO对象以json格式写回页面
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage(),e);
			model.addAttribute("errorMsg", "系统异常，请稍后重试!");
		}
	}
	
	
	/**
	 * 进入新增文化面貌表单页面
	 * @return
	 */
	@RequestMapping(value = "/cultureform")
	public String mcuform(Model model,HttpServletRequest request,HttpServletResponse response,
			@RequestParam(required = false, value = "mRelicsId") Integer mRelicsId,
			@RequestParam(required = false, value = "relicsName") String relicsName,
			@RequestParam(required = false, value = "mCultureId") Integer mCultureId) {
		log.info("进入基本信息表单页面...mCultureId:"+mCultureId);
		try {
			if (mCultureId != null) {
				List<RelicsAttachment> attachments;
				attachments = commonService.selectAttachmentListByKey(
						mCultureId, AttachmentFrom.RELICS_M_CULTRURE_01,
						AttachmentFrom.RELICS_M_CULTRURE_02,
						AttachmentFrom.RELICS_M_CULTRURE_03,
						AttachmentFrom.RELICS_M_CULTRURE_04);
				model.addAttribute("attachments", attachments);
			} else if (mRelicsId != null) {// 查询对应的附件集合
				List<RelicsAttachment> attachments;

				attachments = commonService.selectAttachmentListByKey(
						mRelicsId, "relics_m_culture", "m_culture_id",
						"m_relics_id", AttachmentFrom.RELICS_M_CULTRURE_01,
						AttachmentFrom.RELICS_M_CULTRURE_02,
						AttachmentFrom.RELICS_M_CULTRURE_03,
						AttachmentFrom.RELICS_M_CULTRURE_04);

				model.addAttribute("attachments", attachments);
				model.addAttribute("relicsName",
						URLDecoder.decode(relicsName, "UTF-8"));
			}
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		return "mculture/cultureform";
	}
	
	/**
	 * 进入新增文化面貌添加/修改页面
	 * @return
	 * @throws Exception 
	 * @throws  
	 */
	@RequestMapping(value = "/showMCuform")
	public void updateMcuform(HttpServletResponse response,
			@RequestParam(value = "mRelicsId") Integer mRelicsId) {
		log.info("进入基本信息表单页面...");
		try{
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			jsonGenerator.writeObject(moveableCultureService.selectMCultureByMWenwuKey(mRelicsId));
		}catch(Exception e){
			log.info(e.getMessage(),e);
		}
	}
	
	
	
	/**
	 * 进入新增文化面貌表单页面
	 * @return
	 */
	@RequestMapping(value = "/savecultureform")
	public void savemcuform(RelicsMCulture relicsMCulture,HttpServletResponse response){
		log.info("进入新增文化面貌表单页面...:"+relicsMCulture);
		try {
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			jsonGenerator.writeObject(moveableCultureService.addRelicsMCuture(relicsMCulture));
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage(),e);
		}
	}
	
	
	/**
	 * 进入新增文化面貌添加/修改页面
	 * @return
	 * @throws Exception 
	 * @throws  
	 */
	@RequestMapping(value = "/showMCuformSelft")
	public void updateMcuformSelft(HttpServletResponse response,
			@RequestParam(value = "mCultureId") Integer mCultureId) {
		log.info("进入基本信息表单页面...");
		try{
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			jsonGenerator.writeObject(moveableCultureService.selectMCultureBySelftKey(mCultureId));
		}catch(Exception e){
			log.info(e.getMessage(),e);
		}
	}
	
	
	/**
	 * 进入新增文化面貌添加/修改页面
	 * @return
	 * @throws Exception 
	 * @throws  
	 */
	@RequestMapping(value = "/deleteMCuformSelft")
	public void deleteMcuformSelft(HttpServletResponse response,
			@RequestParam(value = "keys") String keys) {
		log.info("进入基本信息表单页面...");
		try{
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			jsonGenerator.writeObject(moveableCultureService.deleteBykeys(keys));
		}catch(Exception e){
			log.info(e.getMessage(),e);
		}
	}
}
